package dao.Impl;

import dao.BookDao;

import entry.Book;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class BookDaoImpl implements BookDao {

    private DBHelper db = new DBHelper();

    @Override
    public int insertBook(Book book) {
        String sql = "insert into books values(null,?,?,?,?,?,?)";
        return db.updateinfo(sql, book.getBookName(), book.getBookAuthor(), book.getBookPublish(), book.getBookPage(), book.getBookPrice(), book.getCreateDate());
    }

    @Override
    public ArrayList<Book> getBooks() {
        String sql = "select * from books";
        return db.selectinfo(sql, Book.class);
    }

    @Override
    public ArrayList<Book> selectBook(Book book, PageHelper pageHelper) {
        StringBuilder strb = new StringBuilder("select * from books where 1=1 ");
        ArrayList list = new ArrayList();
        if (book.getBookName() != null) {
            strb.append("and BookName like ? ");
            list.add("%" + book.getBookName() + "%");
        } else if (book.getBookAuthor() != null) {
            strb.append("and BookAuthor like ? ");
            list.add("%" + book.getBookAuthor() + "%");
        } else if (book.getBookPublish() != null) {
            strb.append("and BookPublish like ? ");
            list.add("%" + book.getBookPublish() + "%");
        }
        strb.append(" limit ?,?");
        int startIndex = (pageHelper.getIndexPage() - 1) * pageHelper.getShowData();
        list.add(startIndex);
        list.add(pageHelper.getShowData());
        return db.selectinfo(strb.toString(), Book.class, list.toArray());


    }

    @Override
    public Book selectBookById(int id) {
        String sql = "select * from books where bookId=?";
        return (Book) db.selectinfo(sql, Book.class, id).get(0);
    }

    @Override
    public int deleteBook(Book book) {
        String sql = "delete from books where bookId=?";
        return db.updateinfo(sql, book.getBookId());
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update books set bookName=?,bookAuthor=?,bookPublish=?,bookPage=?,bookPrice=?,createDate=? where bookId=?";
        return db.updateinfo(sql, book.getBookName(), book.getBookAuthor(), book.getBookPublish(), book.getBookPage(), book.getBookPrice(), book.getCreateDate(), book.getBookId());
    }

    @Override
    public boolean deleteBook1(int id) {
        String sql = "delete from books where bookId=?";
        return db.updateinfo(sql, id) > 0 ? true : false;


    }

    @Override
    public int selectBookSize(Book book) {
        StringBuilder strb = new StringBuilder("select count(bookId) from books where 1=1 ");
        if (book.getBookName() != null) {
            strb.append("and BookName like ? ");
            return db.selectinfo1(strb.toString(), "%" + book.getBookName() + "%");
        } else if (book.getBookAuthor() != null) {
            strb.append("and BookAuthor like ? ");
            return db.selectinfo1(strb.toString(), "%" + book.getBookAuthor() + "%");
        } else if (book.getBookPublish() != null) {
            strb.append("and BookPublish like ? ");
            return db.selectinfo1(strb.toString(), "%" + book.getBookPublish() + "%");
        } else {
            return db.selectinfo1(strb.toString());
        }
    }
}
